﻿namespace Pub.Class
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Web;

    public class Identify
    {
        public static void DrawIdentifyCode(string strIdentifyCode, int intFgNoise, int intBgNoise)
        {
            if (!strIdentifyCode.IsNull() && (strIdentifyCode.Trim() != string.Empty))
            {
                Bitmap image = new Bitmap((int) Math.Ceiling((double) (strIdentifyCode.Length * 12.5)), 0x16);
                Graphics graphics = Graphics.FromImage(image);
                try
                {
                    Random random = new Random();
                    graphics.Clear(Color.White);
                    for (int i = 0; i < intBgNoise; i++)
                    {
                        int num2 = random.Next(image.Width);
                        int num3 = random.Next(image.Width);
                        int num4 = random.Next(image.Height);
                        int num5 = random.Next(image.Height);
                        graphics.DrawLine(new Pen(Color.Silver), num2, num4, num3, num5);
                    }
                    Font font = new Font("Arial", 12f, FontStyle.Italic | FontStyle.Bold);
                    LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
                    graphics.DrawString(strIdentifyCode, font, brush, (float) 2f, (float) 2f);
                    for (int j = 0; j < intFgNoise; j++)
                    {
                        int x = random.Next(image.Width);
                        int y = random.Next(image.Height);
                        image.SetPixel(x, y, Color.FromArgb(random.Next()));
                    }
                    graphics.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                    MemoryStream stream = new MemoryStream();
                    image.Save(stream, ImageFormat.Gif);
                    HttpContext.Current.Response.ClearContent();
                    HttpContext.Current.Response.ContentType = "image/Gif";
                    HttpContext.Current.Response.BinaryWrite(stream.ToArray());
                }
                finally
                {
                    graphics.Dispose();
                    image.Dispose();
                }
            }
        }

        public static void DrawIdentifyCode2(string checkCode)
        {
            if (!checkCode.IsNull() && (checkCode.Trim() != string.Empty))
            {
                Bitmap image = new Bitmap((int) Math.Ceiling((double) (checkCode.Length * 20.5)), 0x1c);
                Graphics graphics = Graphics.FromImage(image);
                try
                {
                    new Random();
                    graphics.Clear(Color.White);
                    Font font = new Font("Arial", 14f, FontStyle.Bold);
                    LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
                    graphics.DrawString(checkCode, font, brush, (float) 2f, (float) 2f);
                    graphics.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                    MemoryStream stream = new MemoryStream();
                    image.Save(stream, ImageFormat.Gif);
                    HttpContext.Current.Response.ClearContent();
                    HttpContext.Current.Response.ContentType = "image/Gif";
                    HttpContext.Current.Response.BinaryWrite(stream.ToArray());
                }
                finally
                {
                    graphics.Dispose();
                    image.Dispose();
                }
            }
        }

        public static string IdentifyCode(int intLength)
        {
            string str = string.Empty;
            Random random = new Random();
            for (int i = 0; i < intLength; i++)
            {
                char ch;
                int num = random.Next();
                if ((num % 2) == 0)
                {
                    ch = (char) (0x30 + ((ushort) (num % 10)));
                }
                else
                {
                    ch = (char) (0x41 + ((ushort) (num % 0x1a)));
                }
                str = str + ch.ToString();
            }
            Cookie2.Set("IdentifyCode", str);
            return str;
        }

        public static void IdentifyCode2()
        {
            string[] strArray = new string[] { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
            string[] strArray2 = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
            string[] strArray3 = new string[] { "加", "减" };
            string[] strArray4 = new string[] { "=", "等于" };
            IList<string[]> list = new List<string[]> {
                strArray,
                strArray2
            };
            Random random = new Random();
            int num = random.Next(1, 11);
            int num2 = random.Next(1, 11);
            int num3 = 0;
            int index = random.Next(0, 2);
            if (index == 0)
            {
                num3 = num + num2;
            }
            else
            {
                if (num < num2)
                {
                    int num5 = 0;
                    num5 = num;
                    num = num2;
                    num2 = num5;
                }
                num3 = num - num2;
            }
            string checkCode = string.Empty;
            checkCode = list[random.Next(0, 2)][num - 1];
            checkCode = (checkCode + strArray3[index] + list[random.Next(0, 2)][num2 - 1]) + strArray4[random.Next(0, 2)] + "?";
            Cookie2.Set("IdentifyCode", num3.ToString());
            DrawIdentifyCode2(checkCode);
        }
    }
}

